﻿using Admin2024.Application.Contracts.Role;
using Admin2024.Application.Contracts.Role.Dto;
using Admin2024.Application.Contracts.User.Dto;
using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IRoleService;
using Admin2024.Domain.IDomainService.System.IUserService;
using Admin2024.Domain.ObjValue;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Application.Role
{
    public class RoleInfoService : IRoleInfoService
    {
        private readonly IRoleProfileService roleProfileService;

        private readonly IMapper mapper;
        private readonly IJwtService jwtService;

        public RoleInfoService(IRoleProfileService roleProfileService, IMapper mapper,IJwtService jwtService)
        {
            this.roleProfileService = roleProfileService;
            this.mapper = mapper;
            this.jwtService = jwtService;
        }

        //新增角色
        public async Task<DomainResult<RoleDto>> CreateRole(string roleName, string Description)
        {
            var role = await roleProfileService.CreateRole(roleName, Description);
            if (!role.IsSuccess)
            {
                return DomainResult<RoleDto>.Error(role.Message);
            }

            var roleDto = mapper.Map<RoleDto>(role.Data);
            return DomainResult<RoleDto>.Success(roleDto);
        }
        //获取指定角色
        public async Task<DomainResult<RoleDto>> GetRoleById(Guid roleId)
        {
            var role = await roleProfileService.GetRoleById(roleId);
            if (!role.IsSuccess)
            {
                return DomainResult<RoleDto>.Error(role.Message);
            }

            var roleDto = mapper.Map<RoleDto>(role.Data);
            return DomainResult<RoleDto>.Success(roleDto);
        }
        //获取所有角色（分页）
        public async Task<DomainResult<List<RoleDto>>> GetRolesWithPagination(int page, int pageSize)
        {
            var role = await roleProfileService.GetRolesWithPagination(page, pageSize);
            if (!role.IsSuccess)
            {
                return DomainResult<List<RoleDto>>.Error(role.Message);
            }

            var roleDto = mapper.Map<List<RoleDto>>(role.Data);
            return DomainResult<List<RoleDto>>.Success(roleDto);
        }
      

        //删除角色
        public async Task<DomainResult<RoleDto>> RemoveRole(Guid roleId)
        {

            var role = await roleProfileService.RemoveRole(roleId);
            if (!role.IsSuccess)
            {
                return DomainResult<RoleDto>.Error(role.Message);
            }

            var roleDto = mapper.Map<RoleDto>(role.Data);
            return DomainResult<RoleDto>.Success(roleDto);
        }
        //修改角色信息
        public async Task<DomainResult<RoleDto>> UpdateRoleInfo(Guid roleId, RoleUpdateDto roleUpdateDto)
        {

            var appRole = mapper.Map<AppRole>(roleUpdateDto);
            var role = await roleProfileService.UpdateRoleInfo(roleId, appRole);
            if (!role.IsSuccess)
            {
                return DomainResult<RoleDto>.Error(role.Message);
            }

            var roleDto = mapper.Map<RoleDto>(role.Data);
            return DomainResult<RoleDto>.Success(roleDto);
        }
    }
}
